package com.lucy.flow.service.impl;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.PageResultDTO;
import com.lucy.common.exception.BusinessException;
import com.lucy.flow.dao.FlowWorkDAO;
import com.lucy.flow.domain.entity.FlowNodeAssign;
import com.lucy.flow.domain.entity.FlowWork;
import com.lucy.flow.event.FlowWorkDeletedEvent;
import com.lucy.flow.model.BaseModel;
import com.lucy.flow.model.FlowModel;
import com.lucy.flow.service.FlowWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@Slf4j
@Service
public class FlowWorkServiceImpl implements FlowWorkService {
    @Autowired
    FlowWorkDAO dao;

    /**
     * 应用上下文注册
     */
    @Resource
    ApplicationContext context;

    @Override
    public FlowWork getByBizId(String bizId) {
        Optional<FlowWork> flowWorkOp = dao.findByBizId(bizId);
        if (!flowWorkOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return flowWorkOp.get();
    }

    @Override
    public FlowWork getOneUnwrap(String s) {
        Optional<FlowWork> workOp = dao.findById(s);
        if (!workOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return workOp.get();
    }



    @Override
    public PageResultDTO<FlowWork> getAll(Pageable pageable, Example<FlowWork> example) {
        Page<FlowWork> page;

        if (Validator.isEmpty(example)) {
            page = dao.findAll(pageable);
        } else {
            page = dao.findAll(example, pageable);
        }
        /*
          获取分页主体内容
         */
        List<FlowWork> list = page.getContent();
        PageResultDTO<FlowWork> pageResultDTO = new PageResultDTO<>();


        //封装分页请求结果
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

    @Override
    public List<FlowWork> getExample(Example<FlowWork> example) {
        return dao.findAll(example);
    }

    @Override
    public void deleteOne(String id) {
        FlowWork flowWork = getOneUnwrap(id);
        dao.deleteById(id);
        // 事件发布
        context.publishEvent(new FlowWorkDeletedEvent(this,flowWork));
    }

    @Override
    public Optional<FlowWork> getOne(String id) {
        return dao.findById(id);
    }

    @Override
    public FlowWork saveOne(FlowWork o) {
        if (Validator.isNotEmpty(o.getId())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "新建DTO不可携带ID");
        }
        return dao.save(o);
    }

    @Override
    public FlowWork updateOne(FlowWork o) {
        if (Validator.isEmpty(o.getId())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "更新DTO必须携带ID");
        }
        return dao.save(o);
    }

    @Override
    public FlowWork toNext(String flowWorkId) {
        FlowWork flowWork = getOneUnwrap(flowWorkId);

        return null;
    }

    @Override
    public FlowWork toNext(FlowWork flowWork) {
        FlowModel flowModel = flowWork.getFlowModel();
        // 获取当前节点信息
        BaseModel currentNodeModel = flowModel.getNodeModel(flowWork.getCurrentNodeId());
        return null;
    }
}
